package cn.stylefeng.guns.modular.statistic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.statistic.entity.DayStatistic;
import cn.stylefeng.guns.modular.statistic.mapper.DayStatisticMapper;
import cn.stylefeng.guns.modular.statistic.model.params.DayStatisticParam;
import cn.stylefeng.guns.modular.statistic.model.result.DayStatisticResult;
import cn.stylefeng.guns.modular.statistic.model.result.StatisticResult;
import cn.stylefeng.guns.modular.statistic.service.DayStatisticService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuqiang
 * @since 2020-02-21
 */
@Service
public class DayStatisticServiceImpl extends ServiceImpl<DayStatisticMapper, DayStatistic> implements DayStatisticService {

    @Override
    public void add(DayStatisticParam param){
        DayStatistic entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DayStatisticParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(DayStatisticParam param){
        DayStatistic oldEntity = getOldEntity(param);
        DayStatistic newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DayStatisticResult findBySpec(DayStatisticParam param){
        return null;
    }

    @Override
    public List<DayStatisticResult> findListBySpec(DayStatisticParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DayStatisticParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }


    public List<StatisticResult> statisticsDay(DayStatisticParam param){
        List<StatisticResult> list = new ArrayList<>();

        List<DayStatisticResult> dayStatisticResults = this.baseMapper.customList(param);
        Integer startDay = param.getStartTime();
        Integer endDay = param.getEndTime();
        List<Integer> rangeList = getRangeList(startDay, endDay);

        for(Integer day : rangeList){
            DayStatisticResult result = dayStatisticResults.stream().filter(r -> r.getDay().equals(day)).findFirst().orElse(null);
            if(result != null){
                list.add(new StatisticResult(day,result.getCount()));
            }else {
                list.add(new StatisticResult(day,0));
                //添加到数据库
            }
        }

        return list;
    }

    @Override
    public DayStatisticResult findByDay(int day) {
        return this.baseMapper.findByDay(day);
    }

    private List<Integer> getRangeList(Integer startDay, Integer endDay) {
        int sYear = Integer.parseInt(startDay.toString().substring(0, 4));
        int sMonth = Integer.parseInt(startDay.toString().substring(4, 6));
        int sDay = Integer.parseInt(startDay.toString().substring(6,8));
        int eYear = Integer.parseInt(endDay.toString().substring(0, 4));
        int eMonth = Integer.parseInt(endDay.toString().substring(4, 6));
        int eDay = Integer.parseInt(endDay.toString().substring(6,8));
        LocalDate startDate = LocalDate.of(sYear, sMonth,sDay);
        LocalDate endDate = LocalDate.of(eYear, eMonth,eDay);

        List<Integer> list = new ArrayList<>();
        while (endDate.compareTo(startDate) >= 0){
            list.add(Integer.parseInt(startDate.toString().replace("-","")));
            startDate = startDate.plusDays(1);
        }

        return list;
    }

    private Serializable getKey(DayStatisticParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DayStatistic getOldEntity(DayStatisticParam param) {
        return this.getById(getKey(param));
    }

    private DayStatistic getEntity(DayStatisticParam param) {
        DayStatistic entity = new DayStatistic();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
